package com.example.repluginhostmain;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.qihoo360.replugin.RePlugin;
import com.qihoo360.replugin.RePluginApplication;
import com.qihoo360.replugin.RePluginCallbacks;
import com.qihoo360.replugin.RePluginConfig;
import com.qihoo360.replugin.RePluginEventCallbacks;
import com.qihoo360.replugin.model.PluginInfo;


public class MyApplication extends RePluginApplication {
    @Override
    public void onCreate() {
        super.onCreate();
        LiveEventBus
                .config().autoClear(true)
                .lifecycleObserverAlwaysActive(true);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        RePlugin.enableDebugger(base, BuildConfig.DEBUG);
    }

    @Override
    protected RePluginConfig createConfig() {
        RePluginConfig c = new RePluginConfig();
        c.setOptimizeArtLoadDex(true);
        // 允许“插件使用宿主类”。默认为“关闭”
        c.setUseHostClassIfNotFound(true);

        // FIXME RePlugin默认会对安装的外置插件进行签名校验，这里先关掉，避免调试时出现签名错误
        c.setVerifySign(true);

        // 针对“安装失败”等情况来做进一步的事件处理
        c.setEventCallbacks(new HostEventCallbacks(this));

        // FIXME 若宿主为Release，则此处应加上您认为"合法"的插件的签名，例如，可以写上"宿主"自己的。

         RePlugin.addCertSignature("CC:97:14:36:CC:97:DC:F1:4A:2D:02:22:90:7B:C7:99:91:DB:03:B6:F9:03:3C:EB:2E:A8:A8:13:BF:0F:68:B6");

        return c;
    }

    @Override
    protected RePluginCallbacks createCallbacks() {
        return new HostCallbacks(this);
    }

    private class HostCallbacks extends RePluginCallbacks {

        private static final String TAG = "HostCallbacks";

        private HostCallbacks(Context context) {
            super(context);
        }

        @Override
        public boolean onPluginNotExistsForActivity(Context context, String plugin, Intent intent, int process) {
            // FIXME 当插件"没有安装"时触发此逻辑，可打开您的"下载对话框"并开始下载。
            // FIXME 其中"intent"需传递到"对话框"内，这样可在下载完成后，打开这个插件的Activity
            if (BuildConfig.DEBUG) {
                Log.d(TAG, "onPluginNotExistsForActivity: Start download... p=" + plugin + "; i=" + intent);
            }
            return super.onPluginNotExistsForActivity(context, plugin, intent, process);
        }

    }


    private class HostEventCallbacks extends RePluginEventCallbacks {

        private static final String TAG = "HostEventCallbacks";

        public HostEventCallbacks(Context context) {
            super(context);
        }

        @Override
        public void onInstallPluginFailed(String path, RePluginEventCallbacks.InstallResult code) {
            // FIXME 当插件安装失败时触发此逻辑。您可以在此处做“打点统计”，也可以针对安装失败情况做“特殊处理”
            // 大部分可以通过RePlugin.install的返回值来判断是否成功
            Log.d(TAG, "onInstallPluginFailed: Failed! path=" + path + "; r=" + code);
            super.onInstallPluginFailed(path, code);
        }

        @Override
        public void onInstallPluginSucceed(PluginInfo info) {
            super.onInstallPluginSucceed(info);
//            Intent intent = RePlugin.createIntent("image", "com.xq.imageplugindemo.MainActivity") ;
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
//            RePlugin.startActivity(getApplicationContext(),
//                    intent);
        }

        @Override
        public void onStartActivityCompleted(String plugin, String activity, boolean result) {
            // FIXME 当打开Activity成功时触发此逻辑，可在这里做一些APM、打点统计等相关工作
            super.onStartActivityCompleted(plugin, activity, result);
        }
    }

}
